<?php
namespace Home\Controller;
use Think\Controller;

class TaskListController extends Controller {
    
    public function index() {
        //初始化页面的的文章类型列表
        $allContType = M("ContentType")->select();
        $this->assign("typeList", $allContType);

        //  登录用户的id
        $userIdFromSession = $this->getUserIdFromCookieOrSession();
        //  如果$userIdFromSession仍然为空，直接在页面打印提示用户登录的信息
        if (empty($userIdFromSession)) {
        	echo "请重新登录。。。";
            return;
        }

        $this->assign("pageTitle", "任务列表");  //  设置文件的title
        //  初始化显示已经完成才事项,默认显示最新的15条
        //  任务的状态：1-新建；2-完成；3-删除
        $completeList = M('Tasklist')->where(array('msg_statu' => 2, 'userId' => $userIdFromSession))->order('id DESC')->limit(15)->select();
        //  新增
        $newList = M('Tasklist')->where(array('msg_statu' => 1, 'userId' => $userIdFromSession))->order('id DESC')->limit(15)->select();
        //  删除的
        $deleteList = M('Tasklist')->where(array('msg_statu' => 3, 'userId' => $userIdFromSession))->order('id DESC')->limit(15)->select();
        //var_dump($list);
        $this->assign("newList", $newList);
        $this->assign("completeList", $completeList);
        $this->assign("deleteList", $deleteList);
        
        $this->display();
    }
    
    
    /**
     * ajax方式新增一条任务信息
     */
    public function add() {
        $data['task_msg'] = $_POST['task_msg'];
        //任务的状态：1-新建；2-完成；3-删除
        $data['msg_statu'] = 1;  //
        //$userIdFromSession = $_SESSION['LOGINUSERINFO']['user_id'];
        $userIdFromSession = $this->getUserIdFromCookieOrSession();
        $data['userId'] = $userIdFromSession;  // 使用第三方登录的用户id
        $data['record_time'] = date('Y-m-d H:i', time());
        $Model = M('Tasklist');
        $flag = $Model->add($data);
        $json_data = array();
        if ($flag) {
            //  新增成功
            $newList = M('Tasklist')->where(array('msg_statu' => 1, 'userId' => $userIdFromSession))->order('id DESC')->limit(1)->select();
            $json_data = $newList[0];  //把第一条数据以json方式设置到页面
            $json_data['success'] = true;
            //var_dump($json_data);
            echo json_encode($json_data);
        } else {  //  更新失败
            $json_data['success'] = false;
            echo json_encode($json_data);
        }
    }
    
    public function updateStatu() {
        // 更新的类型
        $value = $_GET['value'];  //  更新的值：2-完成；3-删除
        $id = $_GET['id'];
//        $data['id'] = $id;
        $data['msg_statu'] = $value;
        $flag = M('Tasklist')->where(array('id' => $id))->save($data); // 根据条件更新记录
        if (false != $flag) {
        	//  更新成功
            echo '1';
        } else {  //  更新失败
        	// 
        	echo '0';
        }
    }
    
    /**
     * 点击Tab切换的时候动态刷新显示的数据
     */
    public function reflashTabData() {
        $msgType = $_POST['msg_type'];
        if ('new' == $msgType) {  //  新增的
            $this->setList2Json(1);
        } else if ('complete-tab' == $msgType) {  //  已完成的
            $this->setList2Json(2);
        } else {  //  recycle  回收站的
            $this->setList2Json(3);
        }
    }
    
    public function setList2Json($type) {
        //  只显示登录用户的记录
        //$userIdFromSession = $_SESSION['LOGINUSERINFO']['user_id'];
        $userIdFromSession = $this->getUserIdFromCookieOrSession();
        $list = M('Tasklist')->where(array('msg_statu' => $type, 'userId' => $userIdFromSession))->order('id DESC')->limit(15)->select();
        for ($i = 0; $i < count($list); $i++) {
            $array['data'][$i] = $list[$i];
        }
        $array['length'] = $i;
//        $array['success'] = true;
        echo json_encode($array);
    }
    
    // 获取登录用户的 user_id
    public function getUserIdFromCookieOrSession() {
    	$userIdFromSession = $_SESSION['LOGINUSERINFO']['user_id'];
        if (empty($userIdFromSession)) {  //  如果 session 的值为空尝试从 cookie 中获取
        	$userIdFromSession = $_COOKIE['my_cookie_key'];
        }
        
        return $userIdFromSession;
    }

    /**
     * 跳转到使用第三方账号登录页面
     */
    public function login4tasklist() {
        $this->display();
    }

    /**
     * 转到 ember js 的 todo
     */
    public function emberjsTodos() {
        $this->display();
    }
}
